package de.wiroo.setup.web.comp;

import de.stefanocke.roo.wicket.RooEmbed;
import de.stefanocke.roo.wicket.RooFormFields;
import de.stefanocke.roo.wicket.RooProperties;
import de.stefanocke.roo.wicket.RooProperty;
import de.wiroo.setup.domain.Part;
import de.wiroo.setup.web.common.FormFieldsPanel.SimpleFormField;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.validation.validator.StringValidator;

@RooProperties(modelClass = Part.class)
@RooFormFields(modelClass = Part.class)
public class PartFormFields {

    @RooProperty
    public static final String NAME = "name";

    @RooProperty
    public static final String NAME2 = "name2";

    @RooProperty
    @RooEmbed
    @java.lang.Deprecated
    public static final String EMBEDDEDPARTS = "embeddedParts";

    public Component createNameField() {
        SimpleFormField nameField = (SimpleFormField) genCreateNameField();
        nameField.getComponent().add(StringValidator.minimumLength(5));
        return nameField;
    }

    protected void onInitForm(Form<?> form) {
        form.add(new AbstractFormValidator() {

            /***/
            private static final long serialVersionUID = 1L;

            @Override
            public FormComponent<?>[] getDependentFormComponents() {
                return new FormComponent<?>[] { getFormComponent(NAME), getFormComponent(NAME2) };
            }

            @SuppressWarnings("unchecked")
            @Override
            public void validate(Form<?> form) {
                if (!StringUtils.equals(((FormComponent<String>) getFormComponent(NAME)).getConvertedInput(), ((FormComponent<String>) getFormComponent(NAME2)).getConvertedInput())) {
                    error(getFormComponent(NAME2), "name2equals");
                }
            }
        });
    }
}
